package cn.xfdzx.android.apps.shop.fragment.video;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.adapter.HotVideoAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.LiveListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.LiveListContract;
import cn.xfdzx.android.apps.shop.evenbus.VideoListMessage;
import cn.xfdzx.android.apps.shop.presenter.LiveListPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.video.VideoActivity;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author gzk
 * @date 2020/7/4
 * description：我的关注直播
 */
public class MyFocusLiveFragment extends BaseMvpFragment<LiveListPresenter> implements LiveListContract.View {


    RecyclerView rvVideoList;
    private SmartRefreshLayout mRefreshLayout;
    private HotVideoAdapter hotVideoAdapter;
    private int page = 1;
    private List<LiveListBeen.DataBean.ListsBean> mList = new ArrayList<>();
    private LinearLayout ll_liveEmpty;

    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);
        rvVideoList = view.findViewById(R.id.rv_video_list);
        mRefreshLayout = view.findViewById(R.id.refreshLayout);
        ll_liveEmpty = view.findViewById(R.id.ll_car_empty);
        mPresenter = new LiveListPresenter();
        mPresenter.attachView(this);
        mPresenter.getLiveFocusList(false, true, page);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvVideoList.setLayoutManager(linearLayoutManager);
        hotVideoAdapter = new HotVideoAdapter(getActivity(), mList);
        hotVideoAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (EmptyUtil.isEmpty(APP_TOKEN)) {
                    Intent intent = new Intent(getActivity(), OneLoginMainActivity.class);
                    startActivity(intent);
                    return;
                }
                Intent intent = new Intent(getActivity(), VideoActivity.class);
                intent.putExtra("live_id", mList.get(position).getLive().getId() + "");
                intent.putExtra("chat_id", mList.get(position).getLive().getChat_id());
                intent.putExtra("shop_type", mList.get(position).getLive().getSales_model());

                startActivity(intent);
            }
        });
        rvVideoList.setAdapter(hotVideoAdapter);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getLiveFocusList(true, true, page);

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(VideoListMessage messageEvent) {
        if (messageEvent.getMessage() == 1){
            page = 1;
            mPresenter.getLiveFocusList(true, true, page);
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_hot;
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(getActivity());
        }
    }


    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }


    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onLiveListSuccess(LiveListBeen bean) {
    }

    @Override
    public void onLiveFocusListSuccess(LiveListBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (bean.getStatus() == Constants.SUCCESS_CODE) {
                if (page == 1) {
                    mList.clear();
                    mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                        @Override
                        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                            mPresenter.getLiveFocusList(false, false, page);
                        }
                    });
                }
                if (bean.getData().getLists().size() == 0) {
                    mRefreshLayout.finishLoadMoreWithNoMoreData();
                } else {
                    page++;
                }
                mList.addAll(bean.getData().getLists());
                hotVideoAdapter.notifyDataSetChanged();

                if (mList.size() == 0) {
                    ll_liveEmpty.setVisibility(View.VISIBLE);
                } else {
                    ll_liveEmpty.setVisibility(View.GONE);
                }
            } else {
//                Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
            }

        } else {
            //       Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }


    public static MyFocusLiveFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
//        bundle.putString("item", "UserPostFragment");
//        bundle.putLong(Constants.UID, userUid);
        MyFocusLiveFragment fragment = new MyFocusLiveFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
    }

}
